#include "GtkBoard.hpp"
#include <cstdio>

void GtkBoard::create_cheeses(int rows,int columns){
    //创建棋盘并设置棋子的一些基本属性
    chessbtn=new ChessMan[rows*columns];
    int index=0;
    for(int i=0;i<rows;i++){
        for(int j=0;j<columns;j++){
            chessbtn[index].row=i;
            chessbtn[index].column=j;
            chessbtn[index].set_size_request(34,34);
            chessbtn[index].has_chess=false;
            chessbtn[index].disabled=false;
            chessbtn[index].signal_clicked().connect(sigc::bind<ChessMan*>(sigc::mem_fun(*this,
                                               &GtkBoard::chessbtn_clicked),&chessbtn[index]));
            attach(chessbtn[index],j,i);
            chessbtn[index].show();
            index++;
        }
    }
}

void GtkBoard::bind_game(gomoku::player_vs_player *game){//连接Game后端
    game1=game;
}

void GtkBoard::delete_chesses(){
    delete[] chessbtn;
}

void GtkBoard::chessbtn_clicked(ChessMan *chessbtn1){
    //将玩家放置棋盘的位置传输给后端
    chess_position.row=chessbtn1->row;
    chess_position.col=chessbtn1->column;
    if(!chessbtn1->has_chess){//落棋并更新棋盘
        game1->do_operation(chess_position);
        if (!game1->board.change_log.empty()){
            auto change = game1->board.change_log.back();
            switch (change.event){
                case gomoku::point_status::vacant:
                    chessbtn1->set_image_from_icon_name("");
                    chessbtn1->disabled=false;
                    chessbtn1->has_chess=false;
                    break;
                case gomoku::point_status::disabled:
                    chessbtn1->set_image_from_icon_name("point_disabled");
                    chessbtn1->disabled=true;
                    chessbtn1->has_chess=false;
                    break;
                case gomoku::point_status::black:
                    chessbtn1->set_image_from_icon_name("stone_black");
                    chessbtn1->disabled=false;
                    chessbtn1->has_chess=true;
                    break;
                case gomoku::point_status::white:
                    chessbtn1->set_image_from_icon_name("stone_white");
                    chessbtn1->disabled=false;
                    chessbtn1->has_chess=true;
                    break;
            }
        }
    }
    //chessbtn1->set_sensitive(false);//禁用已落棋的位置
}

void GtkBoard::board_clear(int rows,int columns){
    int index;
    for(int i=0;i<rows;i++){
        for(int j=0;j<columns;j++){
            index=i*(rows)+j;
            chessbtn[index].set_image_from_icon_name("");
            chessbtn[index].has_chess=false;
            chessbtn[index].disabled=false;
        }
    }
}

void GtkBoard::update_point(int rows,int columns){
    int index=0;//棋盘位置编号
    for(int i=0;i<rows;i++){
        for(int j=0;j<columns;j++){
            gomoku::point_status status=game1->board[{i,j}];
            switch (status){//初始化棋盘状态
                case gomoku::point_status::vacant:
                    chessbtn[index].set_image_from_icon_name("");
                    chessbtn[index].disabled=false;
                    chessbtn[index].has_chess=false;
                    break;
                case gomoku::point_status::disabled:
                    chessbtn[index].set_image_from_icon_name("point_disabled");
                    chessbtn[index].disabled=true;
                    chessbtn[index].has_chess=false;
                    break;
                case gomoku::point_status::black:
                    chessbtn[index].set_image_from_icon_name("stone_black");
                    chessbtn[index].disabled=false;
                    chessbtn[index].has_chess=true;
                    break;
                case gomoku::point_status::white:
                    chessbtn[index].set_image_from_icon_name("stone_white");
                    chessbtn[index].disabled=false;
                    chessbtn[index].has_chess=true;
                    break;
            }
            index++;
        }
    }
    
}

GtkBoard::~GtkBoard(){
}

void board_msg_box(Gtk::Window &window,const char *msg){
    Gtk::Dialog msg_dialog;
    Gtk::Box *vbox;
    Gtk::Label msg_label;
    //消息框初始化
    msg_dialog.set_transient_for(window);
    msg_dialog.set_icon_name("FiveRow");
    msg_dialog.set_default_size(400,200);
    //消息框内容
    msg_dialog.add_button("确定",Gtk::RESPONSE_OK);
    msg_label.set_label(msg);
    vbox=msg_dialog.get_content_area();
    vbox->pack_start(msg_label);
    vbox->show_all_children();
    msg_dialog.run();
}
